import { get, post } from '../ajax'

import { getAjaxBaseUrl } from '../handler'
import type { STORAGE_MODES } from '@/enum/modules/indicator'

const controller = getAjaxBaseUrl(
  '/applicationModel',
  '/energy',
)

/** 新增或编辑应用模型 */
export function saveOrUpdateApplicationModel(data: {
  id?: number
  name: string
  code: string
}) {
  return post(`${controller}/saveOrUpdateApplicationModel`, data)
}

/** 查询应用模型 */
export function loadApplicationModelList() {
  return get(`${controller}/loadApplicationModelList`)
}

/** 新增或编辑模型节点 */
export function saveOrUpdateModelPoint(data: {
  id?: number
  parentId: number
  name: string
}) {
  return post(`${controller}/saveOrUpdateModelPoint`, data)
}

/** 查询模型节点树 */
export function loadModelPointTreeList(parentId: number) {
  return get(`${controller}/loadModelPointTreeList`, {
    parentId,
  })
}

/** 删除模型或模型节点 */
export function deleteApplicationModel(id: number) {
  return get(`${controller}/deleteApplicationModel`, {
    id,
  })
}

/** 保存模型或模型节点与指标的绑定配置 */
export function saveModelIndicator(data: {
  applicationModelId: number
  indicatorId: number
}[]) {
  return post(`${controller}/saveModelIndicator`, data)
}

/** 查询模型或模型节点与指标的绑定配置 */
export function loadModelOfIndicator(id: number, isSubordinate: boolean = false) {
  return get(`${controller}/loadModelOfIndicator`, {
    id,
    isSubordinate,
  })
}

/** 删除模型或模型节点与指标的绑定配置 */
export function deleteModelIndicator(data: {
  modelId: number
  modelIndicatorId: number
}[]) {
  return post(`${controller}/deleteModelIndicator`, data)
}

/** 查询模型指标拓扑图 */
export function loadApplicationModelTreeList(id: number) {
  return get(`${controller}/loadApplicationModelTreeList`, { id })
}

export function loadTreeList(timeType: STORAGE_MODES, mId: number) {
  return get<any>(`${controller}/loadTreeList/${timeType}/${mId}`)
}

/** 查询模型ID中指标所交集的时间类型 */
export function loadTimeTypeList(mId: number) {
  return get(`${controller}/loadTimeType/${mId}`)
}

/** 从用能单元复制 */

interface NodeList {
  name: string
  pointCode: string
  children: NodeList[]
}
export function copyFromEnergyUnit(modelId: number, includeIndicator: boolean, nodeList: NodeList[]) {
  return post(`${controller}/save/tree/${includeIndicator}/${modelId}`, nodeList)
}
